package com.iqi.onigao.netty;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class NettyHandler extends ChannelInboundHandlerAdapter {

	static final Log log = LogFactory.get(NettyHandler.class);

	
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
	}
	
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		super.channelInactive(ctx);
		NettySession session = ctx.channel().attr(NettySession.sessions).getAndSet(null);
		if (session != null) {
			session.close();
		}
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		super.channelRead(ctx, msg);
		if (msg instanceof byte[]) {
			NettySession session = ctx.channel().attr(NettySession.sessions).get();
			if (session!=null) {
				session.receive((byte[]) msg);
			}
		}
	}
}
